package zw_901_1000.zw_914_卡牌分组;

class Solution {
    public static void main(String[] args) {
        int[] deck = {1,2,3,4,4,3,2,1};
        boolean b = hasGroupsSizeX(deck);
        System.out.println(b);
    }

    public static boolean hasGroupsSizeX(int[] deck) {
        // 计数
        int[] counter = new int[10000];
        for (int num : deck) {
            counter[num]++;
        }
        // 迭代求多个数的最大公约数
        int x = 0;
        for (int cnt : counter) {
            if (cnt > 0) {
                x = gcd(x, cnt);
                if (x == 1) { // 如果某步中gcd是1，直接返回false
                    return false;
                }
            }
        }
        return x >= 2;
    }

    // 辗转相除法
    private static int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }
}
